﻿//********************************************************************************//
//创建日期：<创建日期，2016-10-25>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：来涵井信息 用于井性质与类型修改
//********************************************************************************//
using BLL;
using BLL.Access;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;
using Web.Controls.LAIHAN;

namespace Web.Controls
{
    public partial class UCLHJinInfo2QS : System.Web.UI.UserControl, IUCLHOperate
    {
        #region 属性

        /// <summary>
        /// 来函类型
        /// </summary>
        public eLaiHanLX LaiHanLX
        {
            get;
            set;
        }

        /// <summary>
        /// 当前流程
        /// </summary>
        public eLaiHanLCBH LaiHanLC
        {
            get;
            set;
        }

        /// <summary>
        /// 井编号
        /// </summary>
        public string JINBH
        {
            set
            {
                this.txtJinBH.Text = value;
            }
            get
            {
                return this.txtJinBH.Text;
            }
        }



        /// <summary>
        /// 来函编号
        /// </summary>
        public string LAIHANBH
        {
            set
            {
                this.txtLaiHanBH.Text = value;
            }
            get
            {
                return this.txtLaiHanBH.Text;
            }
        }



        /// <summary>
        /// 判断引用页面类别   1月调整登记  2月调整修改  3其他页面调用 不可修改
        /// </summary>
        public int ControlType
        {
            get { return Convert.ToInt32(ViewState["ControlType"]); }
            set { ViewState["ControlType"] = value; }
        }

        /// <summary>
        /// 记录错误日志
        /// </summary>
        private string errorMsg
        {
            get
            {
                return Convert.ToString(ViewState["errorMsg"]);
            }
            set
            {
                ViewState["errorMsg"] = value;
            }
        }

        ///// <summary>
        ///// 原井类型
        ///// </summary>
        //public string JINLXOLD
        //{
        //    set
        //    {
        //        this.hdCollection.Set("JLX_OLD",value);
        //    }
        //    get
        //    {
        //        return this.hdCollection.Contains("JLX_OLD")?Convert.ToString(this.hdCollection.Get("JLX_OLD")):null;
        //    }
        //}
        ///// <summary>
        ///// 原井状态
        ///// </summary>
        //public string JINXZOLD
        //{
        //    set
        //    {
        //        this.hdCollection.Set("JXZ_OLD", value);
        //    }
        //    get
        //    {
        //        return this.hdCollection.Contains("JXZ_OLD") ? Convert.ToString(this.hdCollection.Get("JXZ_OLD")) : null;
        //    }
        //}

        #endregion

        #region 变量

        #endregion

        #region 事件

        /// <summary>
        /// 加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
        }

        /// <summary>
        /// 回调响应
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult(e.Parameter);
            if (e.Parameter == "bh")//响应用户号回车事件
            {
                DataRowCollection drc = new QS_YH_SHENJINGXXBProcess().SelectQS_YH_SHENJINGXXBsDynamic("S_JINGBH='"+this.txtJinBH.Text+"'",null).Rows;
                //DataRowCollection drc = new SpHelper().P_QS_JINYHXXHQ(null, this.txtJinBH.Text, (int)this.LaiHanLX).Tables[0].Rows;
                if (drc.Count == 0)
                {
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = true;
                    cr.Msg = "查询不到该井信息。";
                    e.Result = cr.ToString();
                    return;
                }
                else
                {

                    if (this.LaiHanLX == eLaiHanLX.水资源井性质修改 || this.LaiHanLX == eLaiHanLX.水资源深井类型修改)
                    {
                        if (Convert.ToInt32(drc[0]["S_JINGZHUANGTBH"]) == (int)eJinXZ.注销许可证||Convert.ToString(drc[0]["S_JIESHUNY"])!="")
                        {
                            cr.IsSuccess = false;
                            cr.IsAlertMsg = true;
                            cr.Msg = "该井目前处于注销状态,无法修改。";
                            e.Result = cr.ToString();
                            return;
                        }
                    }
                    else if(this.LaiHanLX==eLaiHanLX.水资源注销井恢复)
                    {
                        //判断是否为注销许可证
                        if (Convert.ToInt32(drc[0]["S_JINGZHUANGTBH"])!=(int)eJinXZ.注销许可证)
                        {
                            cr.IsSuccess = false;
                            cr.IsAlertMsg = true;
                            cr.Msg = "该井并未注销许可证!";
                            e.Result = cr.ToString();
                            return;
                        }
                    }
                    cr.AddValue(Convert.ToString(drc[0]["S_LAOBIANH"]),Convert.ToString(drc[0]["S_JINGDIZ"]),Convert.ToString(drc[0]["S_JINGLEIXBH"]), Convert.ToString(drc[0]["S_JINGZHUANGTBH"]));
                    //获取相关用户号
                    drc = new QS_YH_YONGHUSJGXBProcess().SelectQS_YH_YONGHUSJGXBsDynamic("S_JINGBIANH='" + this.txtJinBH.Text + "' AND I_JLZT=0", null).Rows;
                    if(drc.Count>0)
                    {
                        cr.AddValue(drc[0]["S_YONGHUBH"].ToString());
                    }
                    cr.IsAlertMsg = false;
                    cr.IsSuccess = true;
                    e.Result = cr.ToString();
                }
            }
            else if (e.Parameter == "add")
            {
                //完整性验证
                ValidateRS rs = this.Validate();
                if(rs.IsSuccess==false)
                {
                    cr.IsAlertMsg = true;
                    cr.IsSuccess = false;
                    cr.Msg = rs.Msg;
                    e.Result = cr.ToString();
                    return;
                }
                //验证是否已添加
                rs = ((UCLHJinListQS)this.Parent.FindControl("UCLHJinListQS")).ValidateAdd(this.txtJinBH.Text);
                if (rs.IsSuccess)
                {
                    cr.IsAlertMsg = false;
                    cr.IsSuccess = true;
                }
                else
                {
                    cr.IsAlertMsg = true;
                    cr.IsSuccess = false;
                    cr.Msg = rs.Msg;
                }
                e.Result = cr.ToString();
            }
        }
        
        /// <summary>
        /// 前台验证后实际执行后台操作
        /// </summary>
        protected void btnActPerform_Click(object sender, EventArgs e)
        {
            /*
            YS_LH_LAIHANXX laihanxx = new YS_LH_LAIHANXX();
            laihanxx.S_LAIHANBH = null;
            laihanxx.I_LAIHANLX = (int)this.LaiHanLX;
            laihanxx.I_LIUCHENGBH = (int)eLaiHanLCBH.未登记;
            laihanxx.S_YONGHUBH = this.txtJinBH.Text;
            laihanxx.S_BEIZHU = this.txtJinBH.Text;
            this.LoadInfo(laihanxx);
            */
            if (this.hdCollection.Get("type").ToString() == "add")
            {
                ((UCLHJinListQS)this.Parent.FindControl("UCLHJinListQS")).Add(this.txtJinBH.Text, null,this.txtJinDZ.Text, this.txtLaoBH.Text, this.cmbJinXZ.Value.ToString(), this.cmbJinXZNEW.Value.ToString(),this.cmbJinLX.Value.ToString(), this.cmbJinLXNEW.Value.ToString());
                this.txtJinBH.Text = "";
                this.txtLaoBH.Text = "";
                this.txtJinDZ.Text = "";
                this.cmbJinLX.SelectedIndex = 0;
                this.cmbJinXZ.SelectedIndex = 0;
                this.cmbJinLXNEW.SelectedIndex = 0;
                this.cmbJinXZNEW.SelectedIndex = 0;
                this.hdCollection.Remove("YHH");
            }
        }
        
        #endregion

        #region 方法

        /// <summary>
        /// 配置控件
        /// </summary>
        public void InitControl()
        {
            this.txtJinBH.ClientEnabled = true;
            this.txtLaiHanBH.ClientEnabled = false;
            this.txtXuKeZH.ClientEnabled = false;
            this.txtLaoBH.ClientEnabled = false;
            PageTools.QSLoadCombox(this.cmbJinLX, PageTools.eLoadType.井类型QS, null, "");
            PageTools.QSLoadCombox(this.cmbJinXZ, PageTools.eLoadType.井性质QS, null, "");
            PageTools.QSLoadCombox(this.cmbJinLXNEW, PageTools.eLoadType.井类型QS, null,"");
            PageTools.QSLoadCombox(this.cmbJinXZNEW, PageTools.eLoadType.井性质QS, null, "");
            this.trJINLX.Visible = (this.LaiHanLX == eLaiHanLX.水资源深井类型修改);
            this.trJINZT.Visible = (this.LaiHanLX == eLaiHanLX.水资源井性质修改);
            //this.dtBianGengRQ.Date = DateTime.Now;
            this.txtJinBH.Focus();
            if(LaiHanLC >= eLaiHanLCBH.业务员)
            {
                this.btnAdd.ClientEnabled = false;
            }
        }

        /// <summary>
        ///根据当前流程状态设置步骤显示
        /// </summary>
        private void SetDisplay()
        {
            /*
            //根据类型显示需要的             
            //受理之后不可编辑.下方switch中case 3可去除.使用此控件的受理之后的页面该控件属性需加上LaiHanLC="受理"
            if ((int)this.LaiHanLC < (int)eLaiHanLCBH.预登记)
            {
                this.txtJinBH.ClientEnabled = true;
                this.cmbJinLX.ClientEnabled = true;
                this.cmbJinXZ.ClientEnabled = true;
                this.dtBianGengRQ.ClientEnabled = true;
            }
            if ((int)this.LaiHanLC >= (int)eLaiHanLCBH.预登记)
            {
                this.txtJinBH.ClientEnabled = false;
                this.cmbJinLX.ClientEnabled = false;
                this.cmbJinXZ.ClientEnabled = false;
                this.dtBianGengRQ.ClientEnabled = false;
            }
            */
        }

        /// <summary>
        /// 验证数据
        /// </summary>
        public ValidateRS Validate()
        {
            ValidateRS rs = new ValidateRS();
            //判断用户号是否存在
            if (this.txtJinBH.Text == "")
            {
                rs.Msg = "请输入井编号。";
                return rs;
            }
            if (this.cmbJinLX.Visible && this.cmbJinLX.Text == "")
            {
                rs.Msg = "请回车查询出井信息。";
                return rs;
            }
            if (this.cmbJinXZ.Visible && this.cmbJinXZ.Text == "")
            {
                rs.Msg = "请回车查询出井信息。";
                return rs;
            }
            if (this.cmbJinLXNEW.Visible && this.cmbJinLXNEW.Text == "")
            {
                rs.Msg = "请选择新井类型。";
                return rs;
            }
            if (this.cmbJinXZ.Visible && this.cmbJinXZNEW.Text == "")
            {
                rs.Msg = "请选择新井性质。";
                return rs;
            } 
            if (this.cmbJinXZ.Visible && (this.cmbJinXZ.Value.ToString() == this.cmbJinXZNEW.Value.ToString()))
            {
                rs.Msg = "井性质未变更,请修改。";
                return rs;
            }
            if (this.cmbJinLX.Visible && (this.cmbJinLX.Value.ToString() == this.cmbJinLXNEW.Value.ToString()))
            {
                rs.Msg = "井类型未变更,请修改。";
                return rs;
            }
            //if (this.dtBianGengRQ.Value==null)
            //{
            //    rs.Msg = "请选择变更日期。";
            //    return rs;
            //}
            rs.IsSuccess = true;
            return rs;
        }

        /// <summary>
        /// 清除数据
        /// </summary>
        public void ClearInfo()
        {
            this.txtJinBH.Text = "";
            this.txtLaiHanBH.Text = "";
            this.txtXuKeZH.Text = "";
            this.txtLaoBH.Text = "";
            this.cmbJinLX.Text = "";
            this.cmbJinXZ.Text = "";
//this.dtBianGengRQ.Text = "";
        }
        public void LoadInfo(YS_LH_LAIHANXX laihanxx)
        {
            if (laihanxx.S_LAIHANBH != null)
            {
                if (this.LaiHanLX == eLaiHanLX.水资源深井类型修改 || this.LaiHanLX == eLaiHanLX.水资源井性质修改 || this.LaiHanLX == eLaiHanLX.水资源注销井恢复)
                {
                    this.Visible = false;
                }
            }
        }
        /*
        /// <summary>
        /// 根据来函加载信息
        /// </summary>
        public void LoadInfo(YS_LH_LAIHANXX laihanxx)
        {
            //登记前
            if (laihanxx.ID == 0)
            {
                this.LaiHanLC = eLaiHanLCBH.未登记;
                SetDisplay();
                return;
            }
            else
            {
                this.LaiHanLC = (eLaiHanLCBH)Enum.Parse(typeof(eLaiHanLCBH), laihanxx.I_LIUCHENGBH.ToString());
                SetDisplay();
            }
            //加载许可号等信息
            this.LAIHANBH = laihanxx.S_LAIHANBH;
            this.txtXuKeZH.Text = laihanxx.S_XUKEBH;
            //加载井信息
            DataRow dr = new SpHelper().P_QS_JINYHXXHQ(laihanxx.S_LAIHANBH, laihanxx.S_BEIZHU, (int)this.LaiHanLX).Tables[0].Rows[0];
            this.txtJinBH.Text = laihanxx.S_BEIZHU;
            this.txtLaiHanBH.Text = laihanxx.S_LAIHANBH;
            this.txtLaoBH.Text = Convert.ToString(dr["S_LAOBIANH"]);
            this.cmbJinLX.Value = Convert.ToString(dr["S_JINGLEIXBH"]);
            this.cmbJinXZ.Value = Convert.ToString(dr["S_JINGZHUANGTBH"]);
            this.JINLXYUAN = Convert.ToString(dr["S_JINLXYUAN"]);
            this.JINZTYUAN = Convert.ToString(dr["S_JINZTYUAN"]);
        }
        */
        /*
        /// <summary>
        /// 获取来函主表信息
        /// </summary>
        /// <param name="laihanid">ID</param>
        /// <param name="liuchengzt">0:收件 1:收件并登记</param>
        /// <param name="msg">输出消息,正常为空</param>
        /// <returns>null:验证失败 !null:成功返回</returns>
        public YS_LH_LAIHANXX GetLAIHANXX(int laihanid, int liuchengzt, out string msg)
        {
            YS_LH_LAIHANXX laihan = new YS_LH_LAIHANXX();
            string laihanbh = "";
            string xukebh = "";
            msg = "";
            laihan.ID = laihanid;
            if (laihanid == 0)
            {
                string zd, glk, ywy;
                PageTools.GetLHDJZDGLKYWY(null, out zd, out glk, out ywy);
                laihan.S_ZHANDIANBH = zd;
                laihan.S_GUANLIKBH = glk;
                laihan.I_YWY = int.Parse(ywy);
                laihan.S_YWY = BusinessTool.GetYWYName(ywy);
                //laihan.S_YHYWY = ywy;
                //DataSet numset = new SpHelper().P_YS_LH_XUKEBH(glk, 1, 0, (BusinessTool.GetWaterYM() / 100).ToString(), out xukebh, out laihanbh);
                new SpHelper().P_ALL_LH_BHSC(BusinessTool.WType, (int)this.LaiHanLX, BusinessTool.GetWaterYM() / 100, out laihanbh);
                laihan.I_XT = BusinessTool.WType;
                laihan.D_SHURURQ = DateTime.Now;
                laihan.D_LAIHANRQ = DateTime.Now;
                laihan.I_JLZT = 0;
                laihan.I_LAIHANLX = (int)this.LaiHanLX;
                laihan.I_SHURUR = LoginInfo.CurrentUser.UserID;
                laihan.I_AUTOTYPE = 0;
                laihan.S_LAIHANBH = laihanbh;
                laihan.S_BEIYONG1 = xukebh;
                laihan.S_XUKEBH = xukebh;
                laihan.I_LIUCHENGZT = (int)eLaiHanLCZT.流程中;
                laihan.I_Y = BusinessTool.GetWaterYM() / 100;
                laihan.I_M = BusinessTool.GetWaterYM() % 100;
                laihan.S_BEIZHU = this.txtJinBH.Text;
                laihan.S_YONGHUBH = null;
            }
            else
            {
                laihan = new YS_LH_LAIHANXXProcess().SelectYS_LH_LAIHANXX(laihan.ID);
            }
            laihan.I_LIUCHENGBH = liuchengzt;
            laihan.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
            laihan.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            return laihan;
        }
        */
     
        /*
        /// <summary>
        /// 添加申请信息
        /// </summary>
        /// <param name="laihanxx"></param>
        public void CreateShengQingXX(YS_LH_LAIHANXX laihanxx)
        {
            if (this.LaiHanLX == eLaiHanLX.水资源深井类型修改)
            {
                //先清除该来函旧的销表水表记录
                QS_LH_JINLXXXProcess bllT2 = new QS_LH_JINLXXXProcess();
                bllT2.DeleteQS_LH_JINLXXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH);
                QS_LH_JINLXXX item = new QS_LH_JINLXXX();
                item.S_JINBH = this.txtJinBH.Text;
                item.S_JINLXXIN = this.cmbJinLX.Value.ToString();
                item.S_JINLXYUAN = this.JINLXYUAN;
                item.S_LAIHANBH = laihanxx.S_LAIHANBH;
                item.D_BIANGENGRQ = this.dtBianGengRQ.Date;
                bllT2.InsertOrUpdate(item);
            }
            else if (this.LaiHanLX == eLaiHanLX.水资源井性质修改)
            {
                //先清除该来函旧的销表水表记录
                QS_LH_JINZTXXProcess bllT2 = new QS_LH_JINZTXXProcess();
                bllT2.DeleteQS_LH_JINZTXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH);
                QS_LH_JINZTXX item = new QS_LH_JINZTXX();
                item.S_JINBH = this.txtJinBH.Text;
                item.S_JINZTXIN = this.cmbJinXZ.Value.ToString();
                item.S_JINZTYUAN = this.JINZTYUAN;
                item.S_LAIHANBH = laihanxx.S_LAIHANBH;
                item.D_BIANGENGRQ = this.dtBianGengRQ.Date;
                bllT2.InsertOrUpdate(item);
            }
        }
        */
        #endregion
    }
}